<template>
  <div>
    <p class="pinformation">搜索条件</p>
    <el-form ref="queryRef" label-width="80" :model="queryParams" :inline="true">
      <el-form-item label="检测类别：" prop="jclbId">
        <el-select v-model="queryParams.jclbId" style="width: 100%" filterable clearable @change="jclbChange">
          <el-option v-for="item in jclbTypeList" :key="item.jclbId"
            :label="item.jclbName !== '' ? item.jclbName : item.jclbGroupName" :value="item.jclbId" />
        </el-select>
      </el-form-item>
      <el-form-item label="点位名称：" prop="pointIds">
        <el-select v-model="queryParams.pointIds" style="width: 100%" filterable clearable multiple collapse-tags
          collapse-tags-tooltip :max-collapse-tags="1">
          <el-option v-for="item in pointTable" :key="item.pointId" :label="item.pointName" :value="item.pointId" />
        </el-select>
      </el-form-item>
      <el-form-item label="检测项目：" prop="jcxmName">
        <el-input @keyup.enter="handleQuery" clearable maxlength="100" v-model="queryParams.jcxmName" />
      </el-form-item>
      <el-form-item label="检测方法：" prop="methodName">
        <el-input @keyup.enter="handleQuery" clearable maxlength="100" v-model="queryParams.methodName" />
      </el-form-item>
      <!-- <el-form-item label="采样天数：" prop="sampleDay">
        <el-input @keyup.enter="handleQuery" clearable maxlength="100" v-model="queryParams.sampleDay" />
      </el-form-item>
      <el-form-item label="每天频次：" prop="sampleCount">
        <el-input @keyup.enter="handleQuery" clearable maxlength="100" v-model="queryParams.sampleCount" />
      </el-form-item> -->
      <el-form-item label="是否分包：" prop="isPackage">
        <el-select clearable v-model="queryParams.isPackage">
          <el-option v-for="dict in isPackageList" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="success" @click="handleQuery">搜索</el-button>
        <el-button class="resetBtn" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row>
      <p class="pinformation">检测内容列表</p>
      <p class="totalStyle">共 {{ total }} 条数据</p>
      <el-row style="margin-left: 20px" class="totalStyle">
        <el-button link type="success" icon="Download" v-if="jchdExcelUrl" @click="handleExportExcel">监测信息</el-button>
      </el-row>
    </el-row>
    <el-table border :data="tableData" ref="dataTableRef" v-loading="loading">
      <el-table-column align="center" width="160" show-overflow-tooltip label="点位检测类别" prop="pointJclbName" />
      <el-table-column align="center" width="170" show-overflow-tooltip label="点位名称" prop="pointName" />
      <el-table-column align="center" width="160" show-overflow-tooltip label="检测项目">
        <template #default="scope">
          <span v-html="proxy.getSupAndSub(scope.row.jcxmName)"></span>
        </template>
      </el-table-column>
      <el-table-column align="center" show-overflow-tooltip label="检测方法" prop="methodName">
        <template #default="scope">
          <span v-html="proxy.getSupAndSub(scope.row.methodName)"></span>
        </template>
      </el-table-column>
      <el-table-column label="是否分包" align="center" width="100" show-overflow-tooltip>
        <template #default="scope">
          {{
            scope.row.isPackage == 0 ? "否" : (scope.row.isPackage == 1 ? "分包采样" : "分包不采样")
          }}
        </template>
      </el-table-column>
      <el-table-column align="center" width="150" show-overflow-tooltip label="分包公司" prop="packageName" />
      <el-table-column align="center" width="85" show-overflow-tooltip label="采样天数" prop="sampleDay" />
      <el-table-column align="center" width="85" show-overflow-tooltip label="每天频次" prop="sampleCount" />
      <el-table-column align="center" width="160" show-overflow-tooltip label="项目检测类别" prop="jclbName" />
    </el-table>
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
  </div>
</template>

<script setup>
import { getJcnrList, getPointTable } from "@/api/taskProgressDetails/index";
import { projectListJclb } from "@/api/store/jclb";
import { getToken } from "@/utils/auth";
import { getConfigKey } from "@/api/system/orgConfig";
const { proxy } = getCurrentInstance();
const queryParams = ref({ pageNum: 1, pageSize: 10, isPackage: -1 });
const tableData = ref([]);
const total = ref(0);
const projectId = ref("");
const loading = ref(false);
const jclbTypeList = ref([]);
const heightParams = ref([{ tableName: "data", bottomHeight: 21 }]);
const jchdExcelUrl = ref();
const isPackageList = ref([
  { value: -1, label: "不限" },
  { value: 0, label: "否" },
  { value: 1, label: "分包采样" },
  { value: 2, label: "分包不采样" },
]);
function getForm(val) {
  projectId.value = val;
  jchdExcelShow();
  queryParams.value.projectId = val;
  projectListJclb({ projectId: val }).then((res) => {
    jclbTypeList.value = res.data;
  });
  getList();
}

function getList() {
  loading.value = true;
  getJcnrList(queryParams.value).then((res) => {
    tableData.value = res.rows;
    total.value = res.total;
    loading.value = false;
  });
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

defineExpose({
  getForm,
});

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}

onMounted(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});

onUpdated(() => {
  nextTick(function () {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "data", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "data", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

const pointTable = ref([]);

function jclbChange(val) {
  // delete queryParams.value.pointId
  if (val) {
    getPointTable({ jclbId: val, projectId: projectId.value }).then((res) => {
      pointTable.value = res.data;
    });
  }
  // else {
  // 	pointTable.value = []
  // }
}

function handleExportExcel() {
  // 使用 $router.push 方法来跳转到指定的页面
  const url = `${import.meta.env.VITE_APP_BASE_API}${jchdExcelUrl.value}?projectId=${projectId.value
    }&token=${getToken()}`;
  window.open(url, "_blank");
}

function jchdExcelShow() {
  getConfigKey("jchd_excel_url").then((res) => {
    if (!res.msg) {
      jchdExcelUrl.value = null;
      return;
    }
    jchdExcelUrl.value = res.msg;
  });
}
</script>

<style></style>
